package com.itheima.servlet;

import com.itheima.bean.User;
import com.itheima.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/selectUserByName")
public class SelectUserByName extends HttpServlet {
    //通过用户名查找用户

    private final UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("UTF-8");
        //获得输入的用户名
        String name = req.getParameter("name");
        User user = userService.selectUserByName(name);
        if(user==null){
        String passWord = req.getParameter("passWord");
            //用户名不存在注册成功  弹窗提示并跳转到向服务器写入用户信息的servlet
            req.setAttribute("passWord",passWord);
            req.setAttribute("name",name);
            req.getRequestDispatcher("addUser").forward(req,resp);
        }else {
            //用户名存在跳回注册页面  并且弹窗
            req.setAttribute("success",0);
            req.getRequestDispatcher("user-register.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
